package com.wch.spit.controller;

import ch.qos.logback.core.util.TimeUtil;
import com.wch.spit.pojo.Spit;
import com.wch.spit.service.SpitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@CrossOrigin
@RequestMapping("/spit")
public class SpitController {

    @Autowired
    private SpitService spitService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询全部数据
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){
        return new Result(true, StatusCode.OK,"查询成功",spitService.findAll());
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Result findOne(@PathVariable String id){
        return new Result(true,StatusCode.OK,"查询成功",spitService.findById(id));
    }

    /**
     * 增加
     * @param spit
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Spit spit){
        spitService.add(spit);
        return new Result(true,StatusCode.OK,"增加成功");
    }


    /**
     * 修改
     * @param spit
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public Result update(@RequestBody Spit spit,@PathVariable String id){
        spit.set_id(id);
        spitService.update(spit);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable String id){
        spitService.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 自定义：根据上级ID查询分页数据
     * @param parentid
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/comment/{parentid}/{page}/{size}",method = RequestMethod.GET)
    public Result findByParentid(@PathVariable String parentid,@PathVariable int page,@PathVariable int size){
        Page<Spit> pageList = spitService.findByParentid(parentid, page, size);
        return new Result(
                true,StatusCode.OK,"查询成功",
                new PageResult<Spit>(pageList.getTotalElements(),pageList.getContent())
        );
    }

    /**
     * 点赞
     * @param id
     * @return
     */
    @RequestMapping(value = "/thumbup/{id}",method = RequestMethod.PUT)
    public Result updateThumbup(@PathVariable String id){
      /*  spitService.updateThumbup(id);
        return new Result(true,StatusCode.OK,"点赞成功");*/

      //采用redis控制，避免重复点赞
        String userid = "2023";  //这个地方后面改成获取当前登录的用户
        if (null != redisTemplate.opsForValue().get("thumbup_"+userid+"_"+id) && !"".equals(redisTemplate.opsForValue().get("thumbup_"+userid+"_"+id))){
            return new Result(false,StatusCode.REPERROR,"你已经点过赞了！");
        }
        spitService.updateThumbup(id);
        redisTemplate.opsForValue().set("thumbup_"+userid+"_"+id,"1");
        return new Result(true,StatusCode.OK,"点赞成功");
    }


    /**
     * 增加浏览量
     * 关于浏览量：可以设置一个时间（此例为2个小时），如果同一个用户在这个时间内多次查看，都只算一次浏览量，
     * 如果两次查看间隔时间超出这个时间，则算两次浏览量
     * 通过设置redis的过期时间来实现
     * @param id
     * @return
     */
    @RequestMapping(value = "/visits/{id}",method = RequestMethod.PUT)
    public Result updateVisits(@PathVariable String id){
        String userid = "2023";  //这里后面要改成获取当前登录的用户
        if(null != redisTemplate.opsForValue().get("visits_"+userid+"_"+id) && !"".equals(redisTemplate.opsForValue().get("visits_"+userid+"_"+id))){
           return new Result(false,StatusCode.REPERROR,"重复浏览，不计入浏览量");
        }
        spitService.updateVisits(id);
        redisTemplate.opsForValue().set("visits_"+userid+"_"+id,"1",2, TimeUnit.HOURS);  //设置过期时间为两小时
        return new Result(true,StatusCode.OK,"浏览量增加");
    }

    /**
     * 增加分享数
     * 分享几次就增加几次，不用考虑同一个用户多次分享的情况，同一个用户多次分享也是算多次分享
     * @param id
     * @return
     */
    @RequestMapping(value = "/share/{id}",method = RequestMethod.PUT)
    public Result updateShare(@PathVariable String id){
        spitService.updateShare(id);
        return new Result(true,StatusCode.OK,"分享成功");
    }


}
